package com.ruitu.mall_98_1.product.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.product.activity.CouponForGiftActivity;
import com.ruitu.mall_98_1.product.activity.CouponListPlatformActivity;
import com.ruitu.mall_98_1.product.activity.ProductDetailActivity;
import com.ruitu.router_module.bean.mine.Coupon;

import java.text.SimpleDateFormat;

public class CouponListAdapter extends ListBaseAdapter<Coupon> {

    public int page = 0;// 0 商品详情页面 1 首页领券中心页面 2 大礼包页面

    public CouponListAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.pro_item_coupon;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {
        TextView tv_sum = holder.getView(R.id.tv_sum);//优惠金额或折扣
        TextView tv_type = holder.getView(R.id.tv_type);//优惠券类型
        TextView tv_limit = holder.getView(R.id.tv_limit);//使用限制
        TextView tv_life = holder.getView(R.id.tv_life);//有限期
        TextView tv_get = holder.getView(R.id.tv_get);//立即领取那几个文字
        LinearLayout ll_get = holder.getView(R.id.ll_get);//立即领取

        final Coupon c = mDataList.get(position);
        if (TextUtils.equals("1", c.getStatus())) {//优惠方式（1，折扣；2，满减）
            tv_sum.setText(c.getDiscountRatio() + "%");
            tv_limit.setText("商品折扣" + c.getDiscountRatio() + "%");//店铺内商品折扣***
        } else {//满减金额
            tv_sum.setText("￥" + c.getReduceMoney());
            tv_limit.setText("商品满" + c.getSatisfyMoney() + "元可使用");//商品满***元可使用
        }

        if (TextUtils.equals("1", c.getType())) {//类型(1，平台；2，企业)
            tv_type.setText("全平台");
        } else {
            if (TextUtils.equals("1", c.getShopType())) {//0优惠券针对店铺 1优惠券针对商品
                tv_type.setText(c.getGoodsName());
            } else {
                tv_type.setText(c.getCompanyName());
            }
        }

        if (c.isHave()) {
            tv_get.setText("已领取");
        } else {
            tv_get.setText("立即领取");
        }

        tv_life.setText("有效期" + new SimpleDateFormat("yyyy-MM-dd").format(Long.parseLong(c.getCreateTime()))
                + "--" + new SimpleDateFormat("yyyy-MM-dd").format(Long.parseLong(c.getUseTime())));

        ll_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (c.isHave()) {
                    ToastUtils.showShort("您已领取过此优惠券");
                    return;
                }
                if (page == 0) {
                    ((ProductDetailActivity) mContext).reqGetCoupon(mDataList.get(position));
                } else if (page == 1) {
                    ((CouponListPlatformActivity) mContext).reqGetCoupon(mDataList.get(position));
                } else if (page == 2) {//大礼包页面
                    ((CouponForGiftActivity) mContext).reqGetCoupon(mDataList.get(position));
                }
            }
        });
    }
}
